package com.sun.alen.controller;


import com.sun.alen.entity.*;
import com.sun.alen.service.TeacherService;
import com.sun.alen.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 老师登录
     * @param teacher
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Teacher teacher){
        Teacher teacher1=teacherService.login(teacher);
        if (teacher1!=null){
            //登录成功，生成令牌，下发令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("teacher",teacher1);
            String jwt= JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }else {
            return Result.error("登录失败");
        }
    }
    /**
     * 获取所有老师
     * @return
     */
    @GetMapping("/all")
    public Result all(){
        List<Teacher> teacherList=teacherService.all();
        return Result.success(teacherList);
    }



    /**
     * 添加老师
     * @param teacher
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Teacher teacher){
        boolean flag=teacherService.add(teacher);
        if (flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改老师
     * @param teacher
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Teacher teacher){
        boolean flag=teacherService.update(teacher);
        if (flag){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 根据ID删除老师
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(String id){
        boolean flag=teacherService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 搜索
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                         @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                         @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                         @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){
        PageBean pageBean=teacherService.search(input,select,currentPage,pageSize);
        return Result.success(pageBean);
    }

    @GetMapping("/fetch")
    public Result getTeacherByPro(String id){
        return Result.success(teacherService.getTeacherByPro(id));
    }

    @PutMapping("/repass")
    public Result repass(@RequestParam("phone") String phone,
                         @RequestParam("pass") String pass,
                         @RequestParam("verify") String verify) {
        int flag = teacherService.repass(phone, pass, verify);
        if (flag == 0) {
            return Result.success();
        } else if (flag == 1) {
            return Result.error("验证码错误");
        } else {
            return Result.error("重置失败");
        }
    }

    /**
     * 获取jwt令牌
     * @param authorizationHeader
     * @return
     */
    @GetMapping("/jwt")
    public Result jwt(@RequestHeader("Authorization")String authorizationHeader){
        String token= authorizationHeader.substring("Bearer".length()).trim();
        //验证JWT令牌
        Claims claims= JwtUtils.parseJWT(token);
        return Result.success(claims);
    }

    /**
     * 修改图片
     * @param teacher
     * @return
     */
    @PutMapping("/img")
    public Result updateImg(@RequestBody Teacher teacher){
        boolean flag=teacherService.updateImg(teacher);
        Teacher teacher1=teacherService.selectById(teacher.getTeacherId());
        if (flag){
            return Result.success(teacher1);
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 修改密码
     * @param teacher
     * @return
     */
    @PutMapping("/password")
    public Result updatePassword(@RequestBody Teacher teacher){
        boolean flag=teacherService.updatePassword(teacher);
        Teacher teacher1=teacherService.selectById(teacher.getTeacherId());
        if (flag){
            return Result.success(teacher1);

        }else {
            return Result.error("修改失败");
        }
    }
}
